package com.lifeng.shop.lifengshop.controller;

import com.lifeng.shop.lifengshop.common.R;
import com.lifeng.shop.lifengshop.pojo.entry.Shopcart;
import com.lifeng.shop.lifengshop.servies.ShopcartServies;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: LiJiaxun
 * @Date: 2021/8/7 14:13
 */

@CrossOrigin
@RestController
@RequestMapping("/shopcart")
public class ShopcartController {
    @Autowired
    ShopcartServies shopcartServies;

    //http://localhost:8082/shopcart/selectUserShopcartByUid?uid=1
    @RequestMapping("/selectUserShopcartByUid")
    public R selectUserShopcartByUid(@RequestParam int uid){
        return new R(200,"成功获取购物车信息",shopcartServies.selectUserShopcartByUid(uid));
    }

    //http://localhost:8082/shopcart/updateProductCount?uid=1&gid=2&count=3
    @RequestMapping("/updateProductCount")
    public R updateProductCount(@RequestParam int uid, @RequestParam int gid, @RequestParam int count){
        Shopcart shopcart = new Shopcart();
        shopcart.setUid(uid);
        shopcart.setGid(gid);
        shopcart.setCount(count);
        return new R(200,"uid："+uid+"更新购物车的gid:"+gid+"号商品，数量："+count,shopcartServies.updateCount(shopcart));
    }

    //http://localhost:8082/shopcart/insertProductToShoppcart?uid=1&gid=6
    @RequestMapping("/insertProductToShoppcart")
    public R insertProductToShoppcart(@RequestParam int uid,@RequestParam int gid,@RequestParam(value = "count",required = false,defaultValue = "1") int count){
        Shopcart shopcart = new Shopcart();
        shopcart.setUid(uid);
        shopcart.setGid(gid);
        shopcart.setCount(count);
        return new R(200,"uid："+uid+"添加gid:"+gid+"号商品到购物车，数量："+count,shopcartServies.insertSelective(shopcart));
    }

    //http://localhost:8082/shopcart/deleteProductFromShopCart?uid=1&gid=6
    @RequestMapping("/deleteProductFromShopCart")
    public R deleteProductFromShopCart(@RequestParam int uid,@RequestParam int gid){
        Shopcart shopcart = new Shopcart();
        shopcart.setUid(uid);
        shopcart.setGid(gid);
        System.out.println(uid + " " +gid);
        return new R(200,"uid："+uid+"删除购物车中gid:"+gid+"号商品",shopcartServies.deleteByGid(shopcart));
    }

    //http://localhost:8082/shopcart/checkProductExist?uid=1&gid=6
    @RequestMapping("/checkProductExist")
    public R checkProductExist(@RequestParam int uid,@RequestParam int gid){
        Shopcart shopcart = new Shopcart();
        shopcart.setUid(uid);
        shopcart.setGid(gid);
        System.out.println(uid + " " +gid);
        return new R(200,"uid："+uid+"删除购物车中gid:"+gid+"号商品",shopcartServies.checkProductExist(shopcart));
    }


}
